const { override, addLessLoader, addWebpackAlias, fixBabelImports, addWebpackPlugin } = require('customize-cra')
const path = require('path')
const webpack = require('webpack')
// const darkThemeVars = require('antd/dist/dark-theme')
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
// const AntDesignThemePlugin = require('antd-theme-webpack-plugin')

// const options = {
//   antDir: path.join(__dirname, './node_modules/antd'), 
//   stylesDir: path.join(__dirname, './src/styles'),
//   varFile: path.join(__dirname, './src/styles/var.less'), 
//   mainLessFile: path.join(__dirname, './src/styles/index.less'), 
//   themeVariables: ['@primary-color'],
//   indexFileName: 'index.html',
//   generateOnce: false,
// }

const resolve = dir => path.join(__dirname, '.', dir)

const rewiredSourceMap = () => (config) => {
  config.devtool = config.mode === 'development' ? 'cheap-module-source-map' : false
  return config
}

const multipleEntry = require('react-app-rewire-multiple-entry')([
  {
    // Webpack extra entry
    entry: 'src/index.tsx',
    // HTML template used in plugin HtmlWebpackPlugin
    // template: 'src/entry/standard.html',
    // // The file to write the HTML to. You can specify a subdirectory
    outPath: 'index.html'
    // // Visit: http[s]://localhost:3000/entry/standard.html
  },
  {
    // Webpack extra entry
    entry: 'src/collectionlist/index.tsx',
    // HTML template used in plugin HtmlWebpackPlugin
    // template: 'src/entry/standard.html',
    // // The file to write the HTML to. You can specify a subdirectory
    outPath: 'collectionlist.html'
    // // Visit: http[s]://localhost:3000/entry/standard.html
  },
  // {
  //   entry: 'src/entry/login.js',
  //   // if [template] is empty, Default value: `public/index.html`
  //   // template: 'public/index.html',
  //   outPath: 'public/login.html'
  //   // Visit: http[s]://localhost:3000/public/login.html
  // },
  // {
  //   entry: 'src/entry/404.js',
  //   template: 'public/404.html'
  //   // if [outPath] is empty, calculated by `path.relative(process.cwd(), template)` --> `public/404.html`
  //   // outPath: '/public/404.html'
  //   // Visit: http[s]://localhost:3000/public/404.html
  // },
  // {
  //   entry: 'src/entry/home.js'
  //   // Default value: `public/index.html`
  //   // template: 'public/index.html',
  //   // Calculated by `path.relative(process.cwd(), template)` --> `public/index.html`
  //   // outPath: '/public/index.html'
  //   // Visit: http[s]://localhost:3000/public/index.html
  // }
]);

module.exports = override(
  multipleEntry.addMultiEntry,
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: true
  }),
  addLessLoader({
    modifyVars: {
      // hack: `true;@import "${require.resolve('antd/lib/style/color/colorPalette.less')}";`,
      // ...darkThemeVars,
      '@primary-color': '#3370da'
    },
    javascriptEnabled: true,
  }),
  addWebpackAlias({
    '~': resolve('src')
  }),
  addWebpackPlugin(
    new webpack.DefinePlugin({'VERSION:':'""'+new Date()+'""'})
  ),
  rewiredSourceMap()
)
